package com.tangkun.environment.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.adapter.SearchHistoryAdapter;
import com.tangkun.environment.adapter.SearchWuRanYuanAdapter;
import com.tangkun.environment.adapter.WuRanYuanAdapter;
import com.tangkun.environment.adapter.ZiXunAdapter;
import com.tangkun.environment.entity.gongzuo.EnterpriseQueryReturn;
import com.tangkun.environment.entity.xinwen.QuestionReturn;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxActivityHelper;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.utils.SharePrefercesUtils;
import com.tangkun.environment.utils.Utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 搜索污染源
 */

public class SearchWuRanYuanActivity extends BaseActivity {

    @BindView(R.id.lv_search)
    ListView listView;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.next)
    TextView next;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.iv_search_delete)
    ImageView ivSearchDelete;

    private Context mContext;
    /**
     * 数据源
     */
    private List<EnterpriseQueryReturn.DataBean> dataList = new ArrayList<EnterpriseQueryReturn.DataBean>();
    /**
     * 适配器
     */
    private SearchWuRanYuanAdapter adapter;

    /**
     * 搜索历史数据源
     */
    private List<String> historyList = new ArrayList<String>();
    /**
     * 搜索历史适配器
     */
    private SearchHistoryAdapter historyAdapter;
    /**
     * 搜索历史，以逗号隔开
     */
    private String searchHistory = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_search_wuranyuan);
        ButterKnife.bind(this);
        initData();
        initListener();
    }

    private void initData() {
        adapter = new SearchWuRanYuanAdapter(mContext, dataList);
        listView.setAdapter(adapter);
    }

    private void initListener() {
        //输入框文字变化监听
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                // 搜索框中文字长度
                if (etSearch.getText().toString().length() > 0) {
                    ivSearchDelete.setVisibility(View.GONE);
                    enterpriseQuery();
                } else {//搜索框没有文字的时候显示所有数据
                    ivSearchDelete.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

    /**
     * 搜索
     */
    private void search() {
        //输入框有内容才执行查询操作
        if (etSearch.getText().toString().trim().length() > 0) {
            enterpriseQuery();
        } else {
            showToast("请输入污染源");
        }
    }

    /**
     * 搜索污染源
     */
    private void enterpriseQuery() {
        fromNetwork.enterpriseQuery(etSearch.getText().toString().trim())
                .compose(new RxActivityHelper<EnterpriseQueryReturn>().ioMain(SearchWuRanYuanActivity.this, true))
                .subscribe(new MySubscribe<EnterpriseQueryReturn>(mContext, true) {
                    @Override
                    protected void success(EnterpriseQueryReturn resultBean) {
                        if (resultBean.data != null) {
                            //填充数据
                            dataList.clear();
                            dataList.addAll(resultBean.data);
                            //刷新列表
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage);
                    }
                });
    }

    @OnClick({R.id.back, R.id.next, R.id.iv_search_delete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back://返回
                Utils.hideSoftKeyboard(SearchWuRanYuanActivity.this, next);
                JumpHelper.finish(mContext);
                break;
            case R.id.next://搜索
                search();
                break;
            case R.id.iv_search_delete://清除按钮
                etSearch.setText("");
                break;
        }
    }
}
